package com.example.rent.controller.zzh;


import com.example.rent.entity.Houseowner;
import com.example.rent.entity.Manager;
import com.example.rent.mapper.zzh.ManagerMapper;
import com.example.rent.service.HouseownerService;
import com.example.rent.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanted
 * @since 2021-12-02
 */
@RestController
@RequestMapping(value = "/manager")
public class ManagerController {
        @Autowired
    ManagerService managerService;
        @Autowired
    ManagerMapper managerMapper;

        @Autowired
        HouseownerService houseownerService;

        @PostMapping("/login")
    public String login(HttpServletRequest request){
            String password1=request.getParameter("password");
            String password=DigestUtils.md5DigestAsHex(password1.getBytes());

            System.out.println(11+password);
            String username=request.getParameter("username");
            Manager manager=managerMapper.getlist(username,password);
            if(manager!=null){
                return "管理员";
            }
            else return "用户名或密码错误";
        }

        @PostMapping("/list")
    public List<Houseowner> getAll(){
            System.out.println(222+DigestUtils.md5DigestAsHex("1234".getBytes()));
            return this.houseownerService.list();
        }

        @PostMapping("/updata")
    public int updata(HttpServletRequest request){
            String oldphone1=request.getParameter("oldphone");
            long oldphone = Long.parseLong(oldphone1);
            String newphone1 = request.getParameter("newphone");
            long newphone = Long.parseLong(newphone1);
            String housename=request.getParameter("housename");
            String address=request.getParameter("address");
            String freetime=request.getParameter("freetime");
            managerMapper.update(oldphone,newphone,housename,address,freetime);
            return 1;
        }

        @PostMapping("/delete")
    public int delete(HttpServletRequest request){
            String phone=request.getParameter("phone");
            long phone1 = Long.parseLong(phone);
            System.out.println(phone);
            managerMapper.delete(phone1);
            return 0;
        }

        @PostMapping("/add")
    public int add(HttpServletRequest request){
        String phone=request.getParameter("phone");
        long phone1 = Long.parseLong(phone);
        String name=request.getParameter("name");
        String address=request.getParameter("address");
        String freetime=request.getParameter("freetime");
        String password1=request.getParameter("password");
        String password=DigestUtils.md5DigestAsHex(password1.getBytes());
        managerMapper.add(phone1,name,address,freetime,password);
            return 0;
        }

        @PostMapping("/export")
    public void export(){

        }


}

